package com.ytjj.common.utils;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.exception.ApiException;
import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;

import javax.crypto.Cipher;
import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @version 1.0
 *
 */
@Slf4j
public class RSADecrypt {

    private final static String prikey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKsvAvQyJjSdY0pB" +
            "sp27/Mg2A5oMchuYg/GHEdYkXngy/hYJp+hvBqNeZvtgH1S4gFHKCrL5r1MH8pFM" +
            "lduKiOygoTHSwy53D/Gk+v+BFUE6nm6Gn/jIIHan4nIc1GN7KzRvG6oMyuaux8sD" +
            "PYHTrnHTi3oRGZqJNX72I+61dvhHAgMBAAECgYEApyvs5Y8H8n/1E/JevV11OgxN" +
            "riYTVVG3IKq4R3kS7K1Xpyvc1sd+IxKw3TH8xt67KaR2mFvv1NVBFJZHDrQWi+5Z" +
            "mYNWBhXwfmdsyGbMtvGPsqH2aFJm8TsKNjeyU4kBua/jo9i3tlEfFdseSLIP8Q5h" +
            "w7IfWtW2X/wFER4Y8UECQQDTbOyN0SlHe/quJIMsYQQ54IlEZryjkrsdtx8T+0yC" +
            "fOmWVsF/Pm0Iu2fEjyQfYxBONuZaQFUQX8XQ2NcOuy4nAkEAz0YmnjjLIHznWEN5" +
            "ACclQRDzEFcW3j3fDR4PPW/f2+Njhh8EHG6o1ih0kFLCnQIeG8v3gldhruzYFWfG" +
            "f/pY4QJAbGUr4omZ4Exv+Vg+6EHVKNc1iRwjGrnftRxtkpEEaOIh4DIagmI/BHsQ" +
            "eHXqzyy67162Ow9AjWSMxsZle+f7gQJAEvNvBHhc2hORc1h377OkjFZoYs4ToS42" +
            "/pQJAK0B4rDfoJulXJQhi7MXNTrb4E8ZQaFcd+ZVYBD3kL10QK87AQJAZWFYVV8y" +
            "RY4MfMtsY7QKvKdprymQyq4YiCTwkTWO6+Fdr0UXn9se6fsF9t98fRJZu3TyHYZr" +
            "nR9eGc6N+sWPzQ==";

    private final static String pubkey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCLBfeAY+ElSYEZa7AehktFL3hcpwf2OjbbBcAMCY+OCwRI95ZIEpv1VgvH4FSx5lmEcSiDnUQum0Ip12+Vy6LQzk+ktjogMSy7C5mfcJErm4fLtJZnxRyBW+mE3b0kdBUhHwi4LON3ECv75sCHgAp9Rzz7k23dS81zMpUzJjdmoQIDAQAB";

    private final static String appkey = "4944ecaf233cf7a49ac36a0a";

    private final static String secret = "70e5a9e93433f00da959518b";

    /**
     * 登录
     *
     * @param loginToken
     * @return
     * @throws Exception
     */
    public static String decrypt(String loginToken) throws Exception {
        String host = "https://api.verification.jpush.cn/v1/web/loginTokenVerify";
        Map<String, Object> map = new HashMap<>();
        map.put("loginToken", loginToken);
        map.put("exID", null);

        String result = HttpRequest.post(host).header("Authorization", "Basic " + Base64.getUrlEncoder().
                encodeToString((appkey + ":" + secret).getBytes())).
        header("Content-Type","application/json; charset=UTF-8")
.body(JSON.toJSONString(map))
                .execute().body();

        log.info("result:[{}]",result);

        JSONObject jsonObject = JSON.parseObject(result);
        log.info("获取phone:{}",JSON.toJSON(jsonObject));
        String cryptograph_phone = (String) jsonObject.get("phone");

        if(StrUtil.isBlank(cryptograph_phone)){
            throw new ApiException("极光认证出错");
        }
        PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(prikey));
        PrivateKey privateKey = KeyFactory.getInstance("RSA").generatePrivate(keySpec);

        Cipher cipher = Cipher.getInstance("RSA");
        cipher.init(Cipher.DECRYPT_MODE, privateKey);

        byte[] b = Base64.getDecoder().decode(cryptograph_phone);

        return new String(cipher.doFinal(b));
    }

}
